const instance = {
  config: {
    baseURL: "http://edu.main.wszjx.cn/",
    token: wx.getStorageSync('token') || '',
  },
  post(url, data, loading = '数据加载中') {
    if (loading != null) {
      wx.showLoading({
        title: loading
      })
    }
    return new Promise((resolve, reject) => {
      wx.request({
        url: this.config.baseURL + url,
        data: data,
        method: 'POST',
        header: {
          "Authorization": wx.getStorageSync('token')
        },
        success(e) {
          let data = e.data;
          resolve(data)
        },
        complete(e) {
          let statusCode = e.statusCode;
          if (statusCode >= 400) {
            wx.showToast({
              title: '网络请求错误',
              icon: 'none'
            })
          } else if (statusCode == 202 && e.data.code == 610) {
            wx.showModal({
              title: '登录认证失效，请重新登录',
              icon: 'none',
              success(res) {
				  if (res.confirm) {
					wx.clearStorage()
					// TODO跳转登录页面重新登录
					wx.navigateTo({
					  url: '/pages/login/index'
					})
				  } else if (res.cancel) {
				  	// console.log('用户点击取消')
				  }
                // wx.getStorageSync('token', '');
                // // TODO跳转登录页面重新登录
                // wx.navigateTo({
                //   url: '/pages/login/index'
                // })
                return;
              }
            })
          }
          if (loading != null) {
            wx.hideLoading()
          }
        }
      })
    })
  },
  get(url, loading = '数据加载中') {
    if (loading != null) {
      wx.showLoading({
        title: loading
      })
    }
    return new Promise((resolve, reject) => {
      wx.request({
        url: this.config.baseURL + url,
        method: 'GET',
        header: {
          "Authorization": wx.getStorageSync('token')
        },
        success(e) {
          let data = e.data;
          resolve(data)
        },
        complete(e) {
          let statusCode = e.statusCode;
          if (statusCode >= 400) {
            wx.showToast({
              title: '网络请求错误',
              icon: 'none'
            })
          } else if (statusCode == 202 && e.data.code == 610) {
            wx.showModal({
              title: '登录认证失效，请重新登录',
              icon: 'none',
              success(res) {
				  if (res.confirm) {
					wx.clearStorage()
					// TODO跳转登录页面重新登录
					wx.navigateTo({
					  url: '/pages/login/index'
					})
				  } else if (res.cancel) {
					// console.log('用户点击取消')
				  }
               
              }
            })
          }
          if (loading != null) {
            wx.hideLoading()
          }
        }
      })
    })
  }
}

export default instance